#include <iostream>
#include <pthread.h>
#include <unistd.h>

using namespace std;

void* ThreadRoutine(void* args){
    while(true){
        cout << "Child Thread pid : " << getpid() << " name : " << (char*)args << endl;
        sleep(1);
    }
}

int main(){

    pthread_t tid;
    pthread_create( &tid, nullptr, ThreadRoutine, (void*)"Thread Cancel");

    // 注意：创建的线程函数使用的是：死循环；此时我们在外部使用 pthread_cancel 函数可以取消线程

    int cnt = 0;
    while(true){
        cout << "Main Thread pid : " << getpid() << endl;
        if(cnt++ == 5) break;
        sleep(1);
    }
    cout << "Main Thread will cancel Child Thread ..." << endl;
    sleep(2);

    pthread_cancel( tid );
    cout << "Pthread cancel : " << tid << endl;

    return 0;
}